package com.example.multilevelapprovalback.listener;

import com.alibaba.excel.converters.Converter;
import com.alibaba.excel.converters.ReadConverterContext;
import com.alibaba.excel.converters.WriteConverterContext;
import com.alibaba.excel.metadata.data.WriteCellData;

import java.time.Year;

public class YearConverter implements Converter<Year> {

    @Override
    public Class<?> supportJavaTypeKey() {
        return Converter.super.supportJavaTypeKey();
    }

    /**
     * 读的时候会调用
     * @param context
     * @return
     * @throws Exception
     */
    @Override
    public Year convertToJavaData(ReadConverterContext<?> context) throws Exception {
        Number yearNumber = context.getReadCellData().getOriginalNumberValue(); // NUMBER类型的年份
        int yearValue = yearNumber.intValue(); // 将NUMBER转换为整数
        Year year = Year.of(yearValue); // 使用整数创建 Year 对象
        return year;
    }

    @Override
    public WriteCellData<?> convertToExcelData(WriteConverterContext<Year> context) throws Exception {
        Year year = context.getValue();
        if (year == null) {
            return new WriteCellData<>("");
        }
        return new WriteCellData<>(year.toString());
    }
}
